package com.ycsj.goldmedalnewconcept.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ycsj.goldmedalnewconcept.R;

public class TitleColorSegmentView extends LinearLayout implements OnClickListener {
	// new的时候用的
	// public SegmentView(Context context) {
	// super(context);
	//
	// }
	// 声明在xml文件中用的
	public TitleColorSegmentView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// 方法的意义:
		// 参数3为null时:将参数2填充形成的view直接返回
		// 参数3不为null时:将参数2填充形成的view加入到参数3中,返回参数3
		View.inflate(context, R.layout.view_segment_titlecolor, this);
		tv_unlock = (TextView) findViewById(R.id.tv_unlock);
		tv_lock = (TextView) findViewById(R.id.tv_lock);
		// 默认让未加锁选项卡选中
		tv_unlock.setSelected(true);

		tv_unlock.setOnClickListener(this);
		tv_lock.setOnClickListener(this);
	}

	// 声明在xml文件中用的,并且带有一些style
	// public SegmentView(Context context, AttributeSet attrs, int defStyle) {
	// super(context, attrs, defStyle);
	//
	// }
	private boolean isUnlockSelected=true;
	private TextView tv_unlock;
	private TextView tv_lock;
	/**
	 * 对外暴露选择状态
	 * @return
	 */
	public boolean isUnlockSelected(){
		return isUnlockSelected;
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_unlock:
			if(!isUnlockSelected){
				tv_unlock.setSelected(true);
				tv_lock.setSelected(false);
				isUnlockSelected=true;
				listener.onSegmentSelected(isUnlockSelected);
			}
			break;
		case R.id.tv_lock:
			if(isUnlockSelected){
				tv_lock.setSelected(true);
				tv_unlock.setSelected(false);
				isUnlockSelected=false;
				listener.onSegmentSelected(isUnlockSelected);
			}
			break;
			
		default:
			break;
		}
//		if(!v.isSelected()){
//			tv_unlock.setSelected((v.getId()==R.id.tv_unlock)&&!isUnlockSelected);
//			tv_lock.setSelected(v.getId()==R.id.tv_lock&&isUnlockSelected);
//			isUnlockSelected=!isUnlockSelected;
//			if(listener!=null){
//				listener.onSegmentSelected(isUnlockSelected);
//			}
//		}
	}
	public OnSegmentSelectedListener listener;
	public interface OnSegmentSelectedListener{
		void onSegmentSelected(boolean isUnlockSelected);
	}
	
	public void setOnSegmentSelectedListener(OnSegmentSelectedListener listener){
		this.listener=listener;
	}

	public void settvtrue() {
		tv_unlock.setSelected(true);
		tv_lock.setSelected(false);
		isUnlockSelected=true;
		listener.onSegmentSelected(isUnlockSelected);
	}
	public void settext(String left,String right) {
		tv_unlock.setText(left);
		tv_lock.setText(right);
	}
	

}
